package com.glowworms.core.web.bind;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import com.glowworms.core.constant.WebConstant;
import com.glowworms.core.util.ObjectUtils;

/**
 * 上传文件对象, 使用方法见 RequestAndResponse 的 getFile方法
 */
public class UploadFile {

	/**
	 * 
	 */
	private MultipartFile multipartFile;

	/**
	 * 
	 * @param multipartFile
	 */
	public UploadFile(MultipartFile multipartFile) {
		super();
		Assert.notNull(multipartFile, "上传对象不能为null");
		this.multipartFile = multipartFile;
	}

	/**
	 * 获取上传对象
	 * 
	 * @return
	 */
	public MultipartFile getMultipartFile() {
		return multipartFile;
	}

	/**
	 * 读取文件字符串
	 * 
	 * @return
	 */
	public String fileToString() {
		String result = null;
		if (ObjectUtils.isNotNull(multipartFile)) {
			try {
				result = IOUtils.toString(multipartFile.getInputStream(), WebConstant.ENCODE);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
}
